/*
 * @Author: szx
 * @Date: 2022-05-17 13:32:59
 * @LastEditTime: 2022-05-17 13:37:04
 * @Description:
 * @FilePath: \leetcode\面试题\04\02\02.js
 */
function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}

/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var sortedArrayToBST = function (nums) {
    const dfs = (start, end) => {
        if (start == end) return new TreeNode(nums[start]);
        const mid = (start + end) >> 1;
        const root = new TreeNode(nums[mid]);
        root.left = dfs(start, end - 1);
        root.right = dfs(mid + 1, end);
        return root;
    };
    return dfs(0, nums.length - 1);
};
